package cn.cwx.code.common.utils;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by GJ on 2017/6/21.
 */
public class SecurityUtils{
    /**
    *@Author  CWX
    *@Date 2017/6/21 17:55
    *@description MD5加密密码
    */
    public static String md5Password(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        if (password==null){
            return "null";
        }
        MessageDigest messageDigest=MessageDigest.getInstance("md5");
        BASE64Encoder base64Encoder=new BASE64Encoder();
        String password_md5=base64Encoder.encode(messageDigest.digest(password.getBytes("utf-8")));
        return password_md5;

    }
    /**
    *@Author  CWX
    *@Date 2017/6/21 18:01
    *@description 与数据库中的密码做对比
     *不匹配返回false，反之相宜
    */
    public static boolean passwordToBase(String password,String basePassword) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String password_md5=md5Password(password);
        if(password_md5.equals(basePassword)){
            return true;
        }else {
            return false;
        }
    }
}
